package com.ys.controller.api.finance;

import com.ys.config.MenuId;
import com.ys.entity.finance.*;
import com.ys.entity.finance.TcwFinancialTransactionType;
import com.ys.entity.finance.TcwFinancialTransactionType;
import com.ys.entity.finance.TcwFinancialTransactionType;
import com.ys.entity.sys.YsCol;
import com.ys.service.finance.TcwFinancialTransactionTypeService;
import com.ys.service.finance.TcwFinancialTransactionTypeService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 财务交易类型
 */
@Controller
@RequestMapping("/api/inter/tcwFinancialTransactionType")
public class ApiInterTcwFinancialTransactionTypeController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTcwFinancialTransactionTypeController.class);

    @Autowired
    private CommonApiService<TcwFinancialTransactionType> commonApiService;


    @Autowired
    private TcwFinancialTransactionTypeService tcwFinancialTransactionTypeService;


    /**
     * 设置查询的cond的sql设置
     * @param cond
     * @param conditions
     * @return
     */
    public TcwFinancialTransactionType setSeachSql(TcwFinancialTransactionType cond, String conditions){

        cond
                .setStatus(1)
                .setOrderby("A.createTime DESC");

        cond = commonApiService.dataAuth(MenuId.TCW_FinancialTransactionType, cond);
        return  cond;
    }
    
//    @RequiresPermissions("tcwFinancialTransactionType:view")
//    @RequestMapping("/search")
//    @ResponseBody
//    public LayuiTableVoJA search(@ModelAttribute TcwFinancialTransactionType cond){
//        cond.addLeftJoin("TCW_FinancialTransactionTypeItem B on  B.Pid = A.id")
//                .addField("B.Vid vid,B.VName vname,B.Vaccountname vaccountname,B.Vremark vremark")
////                .setStatus(1)
//                .where("B.status=1")
//                .setOrderby("A.createTime DESC");
//        return commonApiService.searchLay(cond);
//    }
    /**
     * 查询
     */
    @RequiresPermissions("tcwFinancialTransactionType:view")
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA search(Model model, HttpServletRequest request, HttpServletResponse response,
                                 @ModelAttribute TcwFinancialTransactionType cond,String conditions
    ){
        try {
            cond = setSeachSql(cond,conditions);
            LayuiTableVoJA setList = commonApiService.searchLay(cond);
            return setList;
        }catch (Exception e){
            logger.error("查询出错：",e.getMessage(),e);
            LayuiTableVoJA tableVoJA = new LayuiTableVoJA();
            tableVoJA.setCode(1);
            tableVoJA.setMsg("查询出错，请稍后重试！");
            return tableVoJA;
        }
    }

    /**
     * 查询
     */
    @RequestMapping("/searchByModol")
    @ResponseBody
    public LayuiTableVoJA searchByModol(Model model, HttpServletRequest request, HttpServletResponse response,
                                 @ModelAttribute TcwFinancialTransactionType cond,String conditions
    ){
        try {
            cond = setSeachSql(cond,conditions);
            LayuiTableVoJA setList = commonApiService.searchLay(cond);
            return setList;
        }catch (Exception e){
            logger.error("查询出错：",e.getMessage(),e);
            LayuiTableVoJA tableVoJA = new LayuiTableVoJA();
            tableVoJA.setCode(1);
            tableVoJA.setMsg("查询出错，请稍后重试！");
            return tableVoJA;
        }
    }

    /*
     * 新增
     */
    @RequestMapping("/toAdd")
    @ResponseBody
    @RequiresPermissions("tcwFinancialTransactionType:add")
    public R toAdd(HttpServletRequest request, @RequestParam Map<String, Object> params
    ) {
        try {
            logger.info("财务交易类型新增传参：" + params);
            //修改
            return tcwFinancialTransactionTypeService.add(params);
        } catch (Exception e) {
            logger.error("财务交易类型新增出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/toEdit")
    @ResponseBody
    public R toEdit(HttpServletRequest request, @RequestParam Map<String, Object> params
    ) {
        try {
            logger.info("财务交易类型修改传参：" + params);
            //修改
            return tcwFinancialTransactionTypeService.edit(params);
        } catch (Exception e) {
            logger.error("财务交易类型修改出错：", e.getMessage(), e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }

  
    /**
     * 删除
     * @throws Exception
     */
    @PostMapping("/toDelByItem")
    @ResponseBody
    public R toDelByItem(HttpServletRequest request,@RequestParam("list") List<String> idList){
        try {
            //删除
            for (int i = 0; i < idList.size(); i++) {
                tcwFinancialTransactionTypeService.toDelByItem(Long.parseLong(idList.get(i)));
            }
            return R.ok();
        } catch (Exception e) {
            logger.error("删除出错：",e.getMessage(),e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }
    /**
     * 导出
     */
    @RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TcwFinancialTransactionType cond, String conditions){
        try {
            cond.setPage(0).setSize(999999);
            cond = setSeachSql(cond,conditions);
            //获取菜单字段数据
            List<YsCol> listField = new YsCol()
                    .setMenuId(MenuId.TB_CompanyBankInfoAccount)
                    .setFrontShow(1)
                    .setOrderby("A.SortNo asc")
                    .queryList();
            //获取数据
            List<Map<String, Object>> listData = cond.queryMap();

            //处理数据
            ExportExcelUtils<TcwFinancialTransactionType> util = new ExportExcelUtils<>(new TcwFinancialTransactionType());
            String toFilePath = util.exportExcel(listData, listField, "公司银行账户实时资金管理");
            UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
            return R.ok(dto.getUrl());
        }catch (Exception e){
            logger.error("导出出错：",e.getMessage(),e);
            return R.error("导出失败，请稍后重试！");
        }
    }
}
        










